package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.mapper.DepartmentMapper;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public void delete(long id) {
        departmentMapper.deleteById(id);
    }

    public Department get(Long id) {
        return  departmentMapper.selectById(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectList(null);
    }

    @Override
    public void saveOrUpdate(Department department) {
        if(department.getId() == null){
            departmentMapper.insert(department);
        }else{
            departmentMapper.updateById(department);
        }
    }

    @Override
    public PageResult query(QueryObject qo) {
        int row = departmentMapper.selectForCount(qo);
        if(row == 0){
            return PageResult.EMPTY_PAGE;
        }
        List<Department> data = departmentMapper.selectForList(qo);
        return new PageResult(qo.getCurrentPage(),qo.getPageSize(),data,row);
    }
}
